package com.pay.wx;

import common.ClientUtils;
import common.WXPayUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付~~普通商户支付
 *
 * @author chen
 */
public class WxPay {

    private WXConfig config;

    public WxPay(WXConfig config) throws Exception {
        this.config = config;
        //查看config的值是否存在
        WXPayUtils.checkWXPayConfig(config);
    }

    /**
     * 微信的JSAPI支付
     *
     * @param openId
     * @param orderNum
     * @param money
     * @param notifyUrl
     * @param body
     * @return
     */
    public Map<String, String> wxPayJSAPI(String openId, String orderNum, Integer money, String notifyUrl, String body) throws Exception {
        //微信支付封装参数
        Map<String, String> paramet = new HashMap<>();
        paramet.put("appid", config.getAppID());
        paramet.put("mch_id", config.getMchID());
        paramet.put("nonce_str", WXPayUtils.generateNonceStr());
        paramet.put("body", body);
        //paramet.put("openid", "");
        paramet.put("openid", openId);
        paramet.put("out_trade_no", orderNum);
        paramet.put("total_fee", money.toString());
        paramet.put("spbill_create_ip", "127.0.0.1");
        paramet.put("notify_url",  notifyUrl);
        paramet.put("trade_type", "JSAPI");
        String xmlParam = WXPayUtils.generateSignedXml(paramet, config.getKey());
        System.out.println("微信请求参数：" + xmlParam);
        String xmlResult = ClientUtils.doPostMethod("https://api.mch.weixin.qq.com/pay/unifiedorder", xmlParam, "utf-8");
        System.out.println("微信返回参数：" + xmlResult);
        Map<String, String> wxResultMap = WXPayUtils.xmlToMap(xmlResult);
        //3.获取结果
        if ("SUCCESS".equals(wxResultMap.get("return_code")) && "SUCCESS".equals(wxResultMap.get("result_code"))) {
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("appId", config.getAppID());
            resultMap.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000 / 60));
            resultMap.put("nonceStr", wxResultMap.get("nonce_str"));
            resultMap.put("package", "prepay_id=" + wxResultMap.get("prepay_id"));
            resultMap.put("signType", "MD5");
            String sign = WXPayUtils.generateSignature(resultMap, config.getKey());
            resultMap.put("sign", sign);
            return resultMap;
        } else {
            return wxResultMap;
        }
    }

    /**
     * 微信native支付
     *
     * @param body
     * @param orderNum
     * @param money
     * @param notifyUrl
     * @return
     */
    public Map<String, String> wxNativePay(String body, String orderNum, Integer money, String notifyUrl) throws Exception {
        //微信支付封装参数
        Map<String, String> paramet = new HashMap<>();
        paramet.put("appid", config.getAppID());
        paramet.put("mch_id", config.getMchID());
        paramet.put("nonce_str", WXPayUtils.generateNonceStr());
        paramet.put("body", body);
        paramet.put("out_trade_no", orderNum);
        paramet.put("total_fee", money.toString());
        paramet.put("spbill_create_ip", "127.0.0.1");
        paramet.put("notify_url", notifyUrl);
        paramet.put("trade_type", "NATIVE");
        String xmlParam = WXPayUtils.generateSignedXml(paramet, config.getKey());
        System.out.println("微信请求参数：" + xmlParam);
        String xmlResult = ClientUtils.doPostMethod("https://api.mch.weixin.qq.com/pay/unifiedorder", xmlParam, "utf-8");
        System.out.println("微信返回参数：" + xmlResult);
        Map<String, String> wxResultMap = WXPayUtils.xmlToMap(xmlResult);
        //3.获取结果
        if ("SUCCESS".equals(wxResultMap.get("return_code")) && "SUCCESS".equals(wxResultMap.get("result_code"))) {
            return wxResultMap;
        } else {
            System.out.println("创建订单失败：" + wxResultMap);
            throw new Exception("创建失败");
        }
    }


    /**
     * 查询微信订单
     *
     * @param out_trade_no
     * @return
     * @throws Exception
     */
    public Map<String, String> wxOrderQuery(String out_trade_no) throws Exception {
        Map<String, String> param = new HashMap<>();
        param.put("appid", config.getAppID());
        param.put("mch_id", config.getMchID());
        param.put("out_trade_no", out_trade_no);
        param.put("nonce_str", WXPayUtils.generateNonceStr());
        String xml = WXPayUtils.generateSignedXml(param, config.getKey());
        String xmlResult = ClientUtils.doPostMethod("https://api.mch.weixin.qq.com/pay/orderquery", xml, "utf-8");
        Map<String, String> stringStringMap = WXPayUtils.xmlToMap(xmlResult);
        return stringStringMap;
    }

    /**
     * 微信订单退款
     *
     * @param orderNum
     * @param refundNum
     * @param orderMoney
     * @param refundMoney
     * @return
     * @throws Exception
     */
    public Map<String, String> wxRefund(String orderNum, String refundNum, Integer orderMoney, Integer refundMoney) throws Exception {
        Map<String, String> param = new HashMap<>();
        param.put("appid", config.getAppID());
        param.put("mch_id", config.getMchID());
        param.put("nonce_str", WXPayUtils.generateNonceStr());
        param.put("out_trade_no", orderNum);
        param.put("out_refund_no", refundNum);
        param.put("total_fee", orderMoney.toString());
        param.put("refund_fee", refundMoney.toString());

        String paramXm = WXPayUtils.generateSignedXml(param, config.getKey());
        Map<String, String> resultXml = ClientUtils.certPost("https://api.mch.weixin.qq.com/secapi/pay/refund", paramXm, config.getMchID(), config.getCertStream());
        return resultXml;
    }
}
